package org.swidgets.sgrid;

import java.util.Iterator;
import java.util.List;

import org.swidgets.base.Result;
import org.swidgets.base.SDataModel;
import org.swidgets.base.sbutton.SButton;
import org.swidgets.base.smessagebox.SMessageBox;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;







public class FakeEntryPoint implements EntryPoint{

	public void onModuleLoad() {
		
		AsyncDataReader dataReader = new AsyncDataReader() {
			public void loadTableData(final int start, final int limit,final AsyncCallback callback) {

				AsyncCallback myCallback = new AsyncCallback(){

					public void onFailure(Throwable caught) {
						callback.onFailure(caught);
					}

					public void onSuccess(Object remoteResult) {
						String[][] stringResult = (String[][]) remoteResult;
						
						Result result = new Result();
						result.setData(stringResult);
						result.setCurrentRecords(stringResult.length);
						result.setTotalRecords(30);
						
						callback.onSuccess(result);
					}
				};
				
				TestServiceFactory.getTestServiceAsync().getData(start, limit, myCallback);
			}
			
		}; 
		SDataModel gridModel = new SimpleStringArrayTableModel(){
			
			public String[] getColumnName() {
				return new String[]{"nome","cognome","ruolo"};
			}
			public String[] getColumnWidth(){
				return new String[]{"120px","120px","120px"};
			}
			
		};

		VerticalPanel vp = new VerticalPanel();
		
		final RootPanel rootPanel = RootPanel.get("pagina");
		rootPanel.add(vp);
		final SGrid grid=new SGrid(gridModel,dataReader);
		vp.add(grid);
		grid.setBodyHeight("100px");
		grid.setWidth("360px");
		grid.addTableListener(new TableListener(){
				public void onCellClicked(SourcesTableEvents sender, int row,int cell) {
				String[] dati=(String[])grid.getRowClicked().data;
				String msg="ultima selezione" +dati[2];
				List selectedRows=grid.getSelectedRows();
				Iterator iter=grid.getSelectedRows().iterator();
				String[] crow;
				while(iter.hasNext()){
					crow=(String[])iter.next();
					msg += "\n"+crow[2];
				}
				SMessageBox.alert(msg );
				
			}
			
		});
			
		//grid.load();

		final SButton reloadButton = new SButton();
		rootPanel.add(reloadButton, 106, 187);
		reloadButton.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				grid.load();
			}
		});
		reloadButton.setText("Reload");
	}

}
